<?php
Yii::app()->clientScript->registerScriptFile(Yii::app()->theme->baseUrl . "/js/jquery.number_format.js", CClientScript::POS_END);
$this->pageTitle = 'Manage Products';
$this->breadcrumbs=array(
	UserModule::t('Users')=>array('/user'),
	UserModule::t('Manage'),
);


?>
<div class="heading">
    <h1><img src="<?php echo Util::themeUrl() ?>/images/header.ico" /> Manage Products</h1>

    <div class="controls">
        <?php
        echo CHtml::button('Create', array('class' => 'controls', 'id' => 'create-products'));
        echo CHtml::button('Delete', array('class' => 'controls', 'id' => 'delete-products'));
        ?>
    </div>
</div>

<div class="content">

<?php $this->widget('zii.widgets.grid.CGridView', array(
	'id'=>'products-grid',
	'dataProvider'=>$model->search(),
	'filter'=>$model,
        'cssFile' => Util::themeUrl() . '/css/gridview.css',
        'htmlOptions'=>array('class'=>'grid-view clear'),
        'afterAjaxUpdate'=>'js:function(id,data){$.bind_data()}',
	'columns'=>array(
		array(
            'header' => 'No.',
            'value' => '$row + 1',
            'htmlOptions' => array(
                'style' => 'width:25px; text-align:center'
            )
        ),
		array(
            'class' => 'CCheckBoxColumn',
            'selectableRows' => 2,
            'id' => 'chk',
            'value' => '$data->id',
        ),
		array(
			'name' => 'username',
			'type'=>'raw',
			'value' => 'CHtml::link(Util::markSearch($data,"username"),array("admin/view","id"=>$data->id))',
		),
		array(
			'name'=>'email',
			'type'=>'raw',
			'value'=>'CHtml::link(Util::markSearch($data,"email"), "mailto:".$data->email)',
		),
		'create_at',
		'lastvisit_at',
		array(
			'name'=>'status',
			'value'=>'Util::itemAlias("UserStatus",$data->status)',
			'filter' => Util::itemAlias("UserStatus"),
		),
		array(
                        'class'=>'CButtonColumn',
                        'template' => '{view}{update}',
                        'htmlOptions' => array(
			                'style' => 'width:50px;text-align:center',
			            )

		),
	),
)); ?>
</div>

<script  type="text/javascript">
    $(document).ready(function() {
        $. bind_data = function(){
            $(".grid-view a.view").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('products-grid')
                    }
                });
                return false;
            });

            $(".grid-view a.update").click(function() {
                var url = $(this).attr('href') + '?ajax=true';
                $.fancybox.open({
                    href : url,
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('products-grid')
                    }
                });
                return false;
            });
        }

        // bind data
        $. bind_data();

        $("#create-products").click(function() {
                $.fancybox.open({
                    href : '<?php echo $this->createUrl('create', array('ajax' => true)) ?>',
                    type : 'iframe',
                    afterClose: function(){
                        $.fn.yiiGridView.update('products-grid')
                    }
                });
                return false;
            });

        $('#delete-products').live('click', function(){
            var count=0;
            var params = 'YII_CSRF_TOKEN=<?php echo  Yii::app()->request->csrfToken; ?>';
            $("input[name='chk[]']").each(function(){
            if($(this).is(":checked")){
                count++;
                params += '&chk[]=' + $(this).val();
            }
            });
            if(count >= 1 ){
            if(!confirm("Delete selected records"))
                return false;
            }else{
                alert("No checkbox is selected");
                return false;
            }

            $.ajax({
                    type: "POST",
                    url: '<?php echo $this->createUrl('delete', array('ajax' => true)) ?>',
                    data: params,
                    success: function(data) {
                        $.fn.yiiGridView.update('products-grid');
                    } //success
                });//ajax
                return false;
        });
    });
</script>
